'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	const {authorId,userId} = event
	
	const user = await db.collection('user').doc(userId).get()
	let thumbsUplist = user.data[0].thumbs_up_article_ids
	let count = null
	let msg = ''
	
	let thumbs_up = null
	if(thumbsUplist.includes(authorId)){
		msg = '取消点赞'
		thumbs_up = dbCmd.pull(authorId)
		count = -1
	} else{
		msg = '点赞成功'
		thumbs_up = dbCmd.unshift(authorId)
		count = 1
	}

	await  db.collection('user').doc(userId).update({
		// 向其位置插入或删除执行数据库命令
		thumbs_up_article_ids:thumbs_up
	})
	await  db.collection('article').doc(authorId).update({
		// 向其位置插入或删除
		thumbs_up_count:dbCmd.inc(count)
	})
	//event为客户端上传的参数
	
	
	//返回数据给客户端
	return {
		code:200,
		data:{
			msg
		}
	}
};
